package com.bootdo.mall_part.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.*;
import com.bootdo.mall.domain.MallOrder;
import com.bootdo.mall.service.MallOrderLogService;
import com.bootdo.mall.service.MallOrderService;
import com.bootdo.mall_part.service.MallHousePlotService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/mall_part/statisticController")
public class MallStatisticController extends BaseController {


    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private MallOrderLogService mallOrderLogService;

    @Autowired
    private MallHousePlotService mallHousePlotService;


    @Autowired
    private UserService userService;

    @RequestMapping("getMallOrderIndex")
    @ResponseBody
    public R getMallOrderIndex(@RequestParam Map<String, Object> param) {

        List<MallOrder> mallOrderList = new ArrayList<>();
        UserDO user = ShiroUtils.getUser();


        PageUtils result = new PageUtils(mallOrderList, 0);

//        String startTime = (String) param.get("startTime");
//        String endTime = (String) param.get("endTime");
        String houseFlag = (String) param.get("houseFlag");

//        if (startTime == null && endTime == null) {
//
//            Calendar instance = Calendar.getInstance();
//            instance.setTime(new Date());
//
//            startTime = DateUtils.format(new Date(), "yyyy-MM-dd");
//            String substring = startTime.substring(0, startTime.lastIndexOf("-"));
//            startTime = substring + "-01";
//            endTime = DateUtils.format(new Date(), "yyyy-MM-dd");
//
//            param.put("startTime", startTime);
//            param.put("endTime", endTime);
//        }

        if (user.getUserId() != 1) {
            if (user.getUserType() != null && user.getUserType() == 2) {
                param.put("communityId", user.getCommunityId());
            }

            if (user.getUserType() != null && user.getUserType() == 3) {
                param.put("userId", user.getUserId());
            }
        }

        // 前端可能传过来的参数
        // 起始时间
        // 状态值
        // 商铺的id
        // 社区的id

        // 要返回的参数：list表格
        // 符合条件的总和

        param.put("delFlag", 0);
        Query query = new Query(param);
        mallOrderList = mallOrderService.getSum(query);

        param.remove("limit");
        param.remove("offset");

        List<Integer> total = mallOrderService.getSumCount(param);

        if (houseFlag == null || houseFlag.equals("1")) {
            houseFlag = "1";
        } else {
            houseFlag = "2";
        }

        return R.ok().put("rows", mallOrderList).put("total", total.size()).put("houseFlag", houseFlag);
    }

    @RequestMapping("test")
    @ResponseBody
    public R test() {
        return R.ok().put("1", "hhhh").put("2", "hahaha");
    }


}
